﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VCC.Model;
using VCC.Lib.Common;
using System.Linq.Dynamic;
using VCC.Lib.Paging;
using VCC.Cms.ViewModels;
using VCC.Lib.Helpers;
using VCC.CMS.Properties;
using System.Text.RegularExpressions;
namespace VCC.CMS.Controllers
{
	[RoleHelper(Roles = RoleSystem.ContactUs)]
    public class ContactUsController : BaseController
    {
        //
        // GET: /ContactUs/
        ChoVietDataContext eDataContext = new ChoVietDataContext();
        public ActionResult Index()
        {
            try
            {
                    string order = UserState.getUserStateFromRequest("menu.order", "order", "ID").ToString();
                    string orderDir = UserState.getUserStateFromRequest("menu.orderDir", "orderDir", "desc").ToString();

                    string keyword = UserState.getUserStateFromRequest("menu.Keyword", "Keyword", "").ToString();

                    var pageNumber = Convert.ToInt32(UserState.getUserStateFromRequest("menu.PageNumber", "page", 1));
                    var pageIndex = pageNumber - 1;
                    var pageSize = Convert.ToInt32(UserState.getUserStateFromRequest("menu.PageSize", "pageSize", 20));
                    ViewData["orderDir"] = orderDir;
                    ViewData["order"] = order;
                    ContactUsIndexModel model = new ContactUsIndexModel();
                    model.ContactUss = eDataContext.ContactUs
                                            .OrderBy(string.Format("{0} {1}", order, orderDir))
                                            .ToPagedList(pageIndex, pageSize);
                    return View(model);
            }
            catch (Exception exp)
            {
                return View();
            }
        }

        //
        // GET: /ContactUs/Details/5
        public ActionResult Details(int id)
        {
            try
            {
                ContactUsEditModel model = new ContactUsEditModel();
                model.ContactUs = eDataContext.ContactUs.SingleOrDefault(e => e.ID == id);
                return View(model);
            }
            catch (Exception exp)
            {
                return View();
            }
        }

        // GET: /ContactUs/Delete/5
        public ActionResult Delete(int id)
        {
            try
            {
                    var item = eDataContext.ContactUs.SingleOrDefault(e => e.ID == id);
                    return View(item);
            }
            catch (Exception exp)
            {
               
                return View();
            }
        }

        //
        // POST: /Menu/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                    var item = eDataContext.ContactUs.SingleOrDefault(e => e.ID == id);
                    if (item != null)
                    {
                        eDataContext.ContactUs.DeleteOnSubmit(item);
                        eDataContext.SubmitChanges();
                    }
                    return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                //ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
    }
}
